﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoneyWentWhere.DataModel.Binding;
using System.Diagnostics;

namespace MoneyWentWhere.DataModel
{
    public class ReconcileTxSettings : TxSettings
    {
        public bool BalanceSpecified = false;
        decimal _balance;

        public bool TimeSinceLastRecoSpecified = false;
        TimeSpan _timeSinceLastReco;

        decimal _amount;

        public override TransactionKind Kind
        {
            get { return TransactionKind.Reconcile; }
        }

        public decimal Balance
        {
            get { return _balance; }
            set
            {
                BalanceSpecified = true;
                _balance = value;
            }
        }

        public TimeSpan TimeSinceLastReco
        {
            get { return _timeSinceLastReco; }
            private set
            {
                TimeSinceLastRecoSpecified = true;
                _timeSinceLastReco = value;
            }
        }

        new public static ReconcileTxSettings Parse(String rep)
        {
            ReconcileTxSettings rv = new ReconcileTxSettings();

            decimal balance;
            if  (!rep.Contains("day") &&
                 TryExtractNumber(rep, out balance)) 
            {
                rv.Balance = balance;
            }
            return rv;
        }
        public override string ToRepString()
        {
            if (TimeSinceLastRecoSpecified && 
                TimeSinceLastReco.TotalHours >= 2.4 &&
                _amount != 0) 
            {
                double perDay = -(double)_amount / TimeSinceLastReco.TotalDays;
                if (perDay >= 1 || perDay <= -1)
                {
                    return String.Format("{0} ({1:0}/day * {2:0.#} day{3})",
                        ReconcilePrefix,
                        perDay,
                        TimeSinceLastReco.TotalDays,
                        TimeSinceLastReco.TotalDays == 1 ? "" : "s");
                }
            } 
            if (BalanceSpecified) { return ReconcilePrefix + " " + Balance; }
            return ReconcilePrefix;
        }

        new internal static ReconcileTxSettings Create(Transaction t)
        {
            ReconcileTxSettings rv = new ReconcileTxSettings();
            rv._amount = t.Amount;

            // Find previous reconcile
            TransactionList txList = t.Account.Transactions;
            int index = txList.IndexOf(t);
            for(int i = index - 1; i >= 0; i--)
            {
                if (txList[i].Kind == TransactionKind.Reconcile)
                {
                    rv.TimeSinceLastReco = t.Date - txList[i].Date;
                    break;
                }
            }

            return rv;
        }

        protected override void ApplyCore(Transaction t) 
        {
            decimal balance = t.AccountBalance;
            if (BalanceSpecified) { balance = Balance; }

            t.ReconcileTo(balance);
        }
    }
}
